package cn.wolfcode.luowowo.article.service.impl;

import cn.wolfcode.luowowo.article.domain.*;
import cn.wolfcode.luowowo.article.mapper.ContentMapper;
import cn.wolfcode.luowowo.article.mapper.StrategyCatalogMapper;
import cn.wolfcode.luowowo.article.mapper.StrategyDetailMapper;
import cn.wolfcode.luowowo.article.mapper.StrategyTagMapper;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.cache.service.IStrategyStatsCacheService;
import cn.wolfcode.luowowo.cache.vo.StrategyStats;
import cn.wolfcode.luowowo.common.query.QueryObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Service
public class StrategyDetailServiceImpl implements IStrategyDetailService{

    @Autowired
    private StrategyDetailMapper strategyDetailMapper;
    @Autowired
    private  StrategyCatalogMapper strategyCatalogMapper;
    @Autowired
    private ContentMapper contentMapper;
    @Reference
    private IStrategyStatsCacheService strategyStatsCacheService;
    @Override
    public void deleteByPrimaryKey(Long id) {
        strategyDetailMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void insert(StrategyDetail record) {
        strategyDetailMapper.insert(record);
    }

    @Override
    public StrategyDetail selectByPrimaryKey(Long id) {
        StrategyDetail strategyDetail = strategyDetailMapper.selectByPrimaryKey(id);
        strategyDetail.setStrategyContent(contentMapper.selectByPrimaryKey(strategyDetail.getId()));
        return strategyDetail;
    }

    @Override
    public List<StrategyDetail> selectAll() {
        return strategyDetailMapper.selectAll();
    }

    @Override
    public PageInfo<StrategyDetail> queryForList(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<StrategyDetail> list = strategyDetailMapper.queryForList(qo);
        return new PageInfo(list);
    }

    @Override
    public List<StrategyDetail> getGuideId(Long id) {
        return strategyDetailMapper.getGuideId(id);
    }


    @Override
    public void saveOrUpdate(StrategyDetail strategyDetail, String tags) {
        //生成再要
        strategyDetail.setSummary("人工智能生成");
        //填充攻略名称 和目的地
        StrategyCatalog catalog = strategyCatalogMapper.selectByPrimaryKey(strategyDetail.getCatalog().getId());
        strategyDetail.setStrategyName(catalog.getStrategyName());
        Destination dest = new Destination();
        dest.setId(catalog.getDestId());

        //1.处理文章内容
        StrategyContent content = strategyDetail.getStrategyContent();
        //编辑操作
        if(strategyDetail.getId()==null){
            strategyDetailMapper.insert(strategyDetail);
            content.setId(strategyDetail.getId());
            contentMapper.insert(content);
        //让redis存储 新增文章的热数据
            StrategyStats vo = new StrategyStats();
            vo.setStrategyId(strategyDetail.getId());
            strategyStatsCacheService.setStrategyStats(vo);
        }else{
            strategyDetailMapper.updateByPrimaryKey(strategyDetail);
            content.setId(strategyDetail.getId());
            contentMapper.updateByPrimaryKey(content);
        }
    }

    @Override
    public String getTags(Long id) {
        return strategyDetailMapper.getTags(id);
    }

    @Override
    public List<StrategyDetail> listViewnumTop3(Long destId) {
        return strategyDetailMapper.listViewnumTop3(destId);
    }

    @Override
    public List<StrategyDetail> list() {
        return strategyDetailMapper.list();
    }

    @Override
    public void updateStats(StrategyDetail strategyDetail) {
        strategyDetailMapper.updateStats(strategyDetail);
    }

    @Override
    public void updateByPrimaryKey(StrategyDetail record) {
        strategyDetailMapper.updateByPrimaryKey(record);
    }
}
